﻿using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using NHibernate.Burrow;
using Ornament.MemberShip;
using Ornament.MVCWebFrame.Models;
using StructureMap;

namespace Ornament.MVCWebFrame
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Home", action = "Index", id = ""} // Parameter defaults
                );
            //routes.MapRoute("News", "{controller}/{action}/{type}/{id}",
            //                new {controller = "News", action = "list", type = "金台寺", id = ""});
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            ObjectFactory.Initialize(x => x.PullConfigurationFromAppConfig = true);
            //var bf = new BurrowFramework();
            //try
            //{
            //    bf.InitWorkSpace();
            //    MemberShipContext.Initialize();
            //}
            //finally
            //{
            //    bf.CloseWorkSpace();
            //}
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            //Exception objErr = Server.GetLastError().GetBaseException();
            //string filePath = HttpContext.Current.Request.PhysicalApplicationPath;
            //DateTime now = DateTime.Now;
            //filePath += now.Year + "-" + now.Month + "-" + now.Day + ".txt";
            //TextWriter sw = null;
            //try
            //{
            //    sw = TextWriter.Synchronized(new StreamWriter(File.Open(filePath, FileMode.Append)));
            //    sw.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
            //    if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
            //    {
            //        sw.WriteLine("Login User is " + HttpContext.Current.User.Identity.Name);
            //    }
            //    Exception ex = objErr;
            //    while (ex != null)
            //    {
            //        sw.WriteLine("message:" + ex.Message);
            //        sw.WriteLine("trace:" + ex.StackTrace);
            //        ex = ex.InnerException;
            //    }
            //    sw.WriteLine("---------------------------------------------------------------------------------");
            //    sw.Flush();
            //}
            //finally
            //{
            //    sw.Close();
            //}
            ////additional actions...
        }
    }
}